package com.zzc.legou.service.item.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzc.legou.core.service.impl.CrudServiceImpl;
import com.zzc.legou.item.entity.SpecGroup;
import com.zzc.legou.item.entity.SpecParam;

import com.zzc.legou.log.aop.annotation.LogAnno;
import com.zzc.legou.service.item.dao.SpecGroupDao;
import com.zzc.legou.service.item.dao.SpecParamDao;
import com.zzc.legou.service.item.service.ISpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ISpecGroupServiceImpl extends CrudServiceImpl<SpecGroup> implements ISpecGroupService {

    @Autowired
    private SpecParamDao specParamDao;

    @LogAnno(description = "注册接口",type = "注册")
    @Override
    public void saveGroup(Long aLong, List<SpecGroup> list) {
        //删除sepectGroup组的信息 批量删除信息
        this.baseMapper.delete(Wrappers.lambdaQuery(SpecGroup.class).eq(SpecGroup::getCid, aLong));
        //删除spectParam信息
        specParamDao.delete(Wrappers.lambdaQuery(SpecParam.class).eq(SpecParam::getCid, aLong));
        //进行保存
        for (SpecGroup specGroup : list) {
            /**
             * 实现将id返回给实体类中
             */
            this.baseMapper.insert(specGroup);
            if (!ObjectUtils.isEmpty(specGroup.getParams())) {
                for (SpecParam param : specGroup.getParams()) {
                    param.setGroupId(specGroup.getId());
                    specParamDao.insert(param);
                }
            }

        }
    }

    @Override
    public List<SpecGroup> list(SpecGroup entity) {
        //先进行转换操作
        return ((SpecGroupDao) getBaseMapper()).selectList(entity);
    }
}
